﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;

namespace V5_PhoneLibs.Core {
    /*
     1.需要修改自动创建目录的功能
     */
    public class FsoHelper {

        private static readonly FsoHelper m_Instance = new FsoHelper();
        /// <summary>
        /// 
        /// </summary>
        public static FsoHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="filePath">myFile.txt</param>
        /// <param name="fileContent">This is some text data to be saved in a new text file in the IsolatedStorage!</param>
        public void Write(string filePath, string fileContent) {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            //create new file
            using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.Create, FileAccess.Write, myIsolatedStorage))) {
                string someTextData = fileContent;
                writeFile.WriteLine(someTextData);
                writeFile.Close();
            }
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="filePath">myFile.txt</param>
        /// <param name="fileContent">This is some text data to be saved in a new text file in the IsolatedStorage!</param>
        public void Write2(string filePath, string fileContent) {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            //Open existing file
            IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Write);
            using (StreamWriter writer = new StreamWriter(fileStream)) {
                string someTextData = fileContent;
                writer.Write(someTextData);
                writer.Close();
            }
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Read(string filePath) {
            string bookContent = string.Empty;
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read);
            using (StreamReader reader = new StreamReader(fileStream)) {
                bookContent = reader.ReadToEnd();
            }
            return bookContent;
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool IsFileExists(string filePath) {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!myIsolatedStorage.FileExists(filePath)) {
                return false;
            }
            return true;
        }
    }
}
